/*
 * Copyright 2010 Aekold Helbrass
 * 
 * This file is part of RosterComposer.
 * 
 * RosterComposer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RosterComposer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RosterComposer.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.heresylabs.rostercomposer.storage;

import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.zip.GZIPOutputStream;
import org.heresylabs.rostercomposer.formatter.Formatter;

/**
 *
 * @author Aekold Helbrass <Helbrass@gmail.com>
 */
public class RosterSaver {

    public void writeText(File file, Formatter formatter) {
        try {
            PrintWriter writer = new PrintWriter(file);
            formatter.write(writer);
            writer.flush();
            writer.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void writeObject(File file, Object storage) {
        XMLEncoder encoder = null;
        try {
            encoder = new XMLEncoder(new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(file))));
            encoder.writeObject(storage);
            encoder.flush();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (encoder != null) {
                encoder.close();
            }
        }
    }

}
